package restaurant.core;

import jade.core.AID;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import jade.wrapper.StaleProxyException;

import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JOptionPane;

import restaurant.gui.Agent;
import restaurant.gui.Coordinate;

/**
 * Singleton object which contains the main properties of the restaurant
 * @author Mimmo
 *
 */
public class Restaurant {
	
	private static Restaurant restaurant = null;
	
	public Hashtable<Integer, Table> tables;
	public Hashtable<Integer, Meal> menu;
	
	public Vector<Hashtable<Request, Boolean>> readyMeals;
	public Vector<Hashtable<Request, Boolean>> bills;
	public Vector<Hashtable<Integer, Boolean>> calls;
	
	public int waitersNumber;
	
	public int bookingAgentScore = 0;
	public int kitchenAgentScore = 0;
	public float masterWaiterAgentScore = 0;
	public Hashtable<AID, Double> agentsScore;
	public Hashtable<AID, Coordinate> agentsGoal;
	
	private Restaurant(int tables, Meal[] meals, int waitersNumber, Hashtable<Integer, Coordinate> tablesCoordinates) {
		this.readyMeals = new Vector<Hashtable<Request,Boolean>>();
		this.bills = new Vector<Hashtable<Request,Boolean>>();
		this.calls = new Vector<Hashtable<Integer,Boolean>>();
		
		this.tables = new Hashtable<Integer, Table>();
		for(int i = 0; i < tables; i++) {
			this.tables.put(i, new Table(true, tablesCoordinates.get(i)));
		}
		
		this.menu = new Hashtable<Integer, Meal>();
		for(int i = 0; i < meals.length; i++) {
			this.menu.put(i, meals[i]);
		}
		
		this.waitersNumber = waitersNumber;
		
		this.bookingAgentScore = 0;
		this.kitchenAgentScore = 0;
		this.masterWaiterAgentScore = 0;
		this.agentsGoal = new Hashtable<AID, Coordinate>();
		this.agentsScore = new Hashtable<AID, Double>();
	}
		
	public static Restaurant getInstance(int tables, Meal[] meals, int waitersNumber, Hashtable<Integer, Coordinate> tablesCoordinates) {
		if(restaurant == null) {
			restaurant = new Restaurant(tables, meals, waitersNumber, tablesCoordinates);
			return restaurant;
		}
		return restaurant;
	}
	
	public static Restaurant getInstance() {
		return restaurant;
	}
	
	public static void addCustomer(String customerName) {
		Runtime rt = Runtime.instance();
		ProfileImpl p = new ProfileImpl();
		ContainerController cc = rt.createMainContainer(p);
		AgentController customerController;
		try {
			customerController = cc.createNewAgent(customerName, "restaurant.agents.CustomerAgent", null);
			customerController.start();
		} catch (StaleProxyException e) {
			e.printStackTrace();
		}
	}
	
	public static void goTo(Agent agent, String message, String agentName, AID agentAID, Coordinate coordinate) {
		agent.setGoalReached(false);
		if(message != null)
			JOptionPane.showMessageDialog(null, message, agentName, JOptionPane.INFORMATION_MESSAGE);
		restaurant.agentsGoal.put(agentAID, coordinate);
		while(!agent.obiettivo())  { 
			System.out.print("");
		}
	}
}